import random as r


class Tortoise:
    step = 2  # 步长
    axis = [0, 0]  # 初始化坐标
    area = [0, 0]  # 初始化游戏场景大小

    def __init__(self, a, b):  # 定义游戏场景大小
        self.area[0] = a
        self.area[1] = b

    def start_axis(self, x=0, y=0):  # 设定乌龟开始的坐标
        self.axis[0] = x
        self.axis[1] = y
        return self.axis

    def move(self):  # 乌龟的移动
        while True:
            temp_x = r.randint(-self.step, self.step)
            temp_y = r.randint(-self.step, self.step)
            if temp_x == 0 and temp_y == 0:
                continue
            self.axis[0] = self.axis[0] + temp_x
            self.axis[1] = self.axis[1] + temp_y
            for i in range(0, 2):  # 当移动到边界时往反方向移动
                if self.axis[i] < 0:  # 0 --> -2 = 0 --> 2
                    self.axis[i] = 0 - self.axis[i]
                if self.axis[i] > 10:  # 10 --> 12 = 10 --> 8
                    self.axis[i] = 20 - self.axis[i]
            return self.axis


class Fish:
    step = 1  # 步长
    area = [0, 0]  # 初始化坐标
    axis = [0, 0]  # 初始化游戏场景大小

    def __init__(self, a, b):  # 定义游戏场景大小
        self.area[0] = a
        self.area[1] = b

    def start_axis(self):  # 设定鱼开始的坐标
        self.axis[0] = r.randint(self.area[0], self.area[1])
        self.axis[1] = r.randint(self.area[0], self.area[1])
        return self.axis

    def move(self, axis):  # 鱼的移动
        self.axis = axis
        while True:
            temp_x = r.randint(-self.step, self.step)
            temp_y = r.randint(-self.step, self.step)
            if temp_x == 0 and temp_y == 0:
                continue
            self.axis[0] = self.axis[0] + temp_x
            self.axis[1] = self.axis[1] + temp_y
            for i in range(0, 2):  # 当移动到边界时往反方向移动
                if self.axis[i] < 0:  # 0 --> -1 = 0 --> 1
                    self.axis[i] = 0 - self.axis[i]
                if self.axis[i] > 10:  # 10 --> 11 = 10 --> 9
                    self.axis[i] = 20 - self.axis[i]
            return self.axis


# 游戏开始
energy = 100
tort = Tortoise(1, 10)
tort_axis = tort.start_axis(3, 10)  # 将乌龟放入场景内，设定初始坐标为(3,10)
print('-----------------------------')
print('乌龟的初始位置是：', tort_axis)
# print('乌龟的体力：%d' % energy)
fish = Fish(1, 10)
fish_axis = {}
flag = 1
while True:  # 将10条鱼分别放入不同的坐标中
    temp_axis = fish.start_axis()
    if flag == 1 and temp_axis != tort_axis:
        fish_axis.setdefault(flag, 0)
        fish_axis[flag] = temp_axis.copy()
        print('鱼%d的位置是：' % flag, fish_axis[flag])
        flag += 1
    if flag > 1 and temp_axis != fish_axis[flag - 1] and temp_axis != tort_axis:
        fish_axis.setdefault(flag, 0)
        fish_axis[flag] = temp_axis.copy()
        print('鱼%d的位置是：' % flag, fish_axis[flag])
        flag += 1
    if flag > 10:
        break
print('----------游戏开始-----------')
# 乌龟和小鱼的移动
while True:
    num = list(fish_axis.keys())
    length = len(num)
    for i in range(0, length):  # 小鱼的移动
        flag = 0
        while True:
            temp_axis = fish.move(fish_axis[num[i]])
            if i == 0:
                fish_axis[num[i]] = temp_axis.copy()
                print('鱼%d的位置是：' % num[i], fish_axis[num[i]])
                flag = 1
            elif temp_axis != fish_axis[num[i - 1]]:
                # 字典中的前一项而不是i-1
                fish_axis[num[i]] = temp_axis.copy()
                print('鱼%d的位置是：' % num[i], fish_axis[num[i]])
                flag = 1
            if flag == 1:
                break
    tort_axis = tort.move()  # 乌龟的移动
    print('乌龟的位置是：', tort_axis)
    energy -= 1

    # 判断小鱼是否被乌龟吃掉
    for i in num:
        if fish_axis[i] == tort_axis:
            print('鱼%s被吃掉' % i)
            fish_axis.pop(i)
            print('乌龟体力增加20')
            energy += 20
    print('乌龟的体力：%d' % energy)

    print('-----------------------------')
    if fish_axis == {}:
        print('鱼儿都被吃掉啦，乌龟获胜！')
        break
    elif energy == 0:
        print('乌龟体力不支，小鱼获胜')
        break
